<?
  include_once ("include/ini.php");
  include_once "include/auth_bridge.php";

	$act = $_POST['jrn_act'] ? $_POST['jrn_act'] : $_GET['jrn_act'];
	if (!$act)
	{
		$act = 'read';
	}
	$uid = $_GET['uid'] ? $_GET['uid'] : 0;

	if ($userdata['user_type'] != 2 && !$uid)
	{
		$uid = $userdata['user_id'];
	}
	$isMember = ($userdata['user_type'] != 2);
	$tpl = 'journal.tpl';

	// journal
	$jrnId = $_GET['jrn_id'];
	if ($jrnId)
	{
		$currentUid = $c_journal->selectUserByJournal($jrnId);
		$isOwner = ($currentUid[0]['user_id'] == $userdata['user_id']);
// TODO: get user data
//		$user_data = get_userdata($currentUid);
	}
	else
	{
		if ($uid)
		{
			$user_data = get_userdata($uid);
		}
		if ($isMember)
		{
			$isOwner = true;
		}
	}
if ($isMember)// && $userdata['user_id'] == 1)
{
$isOwner = true;
}
	$text = '';
	// -----
	//if ($uid)
	//{
		$beginUid = $currentUid ? $currentUid[0]['user_id'] : $uid;
		$journalsAr = $c_journal->selectJournalByUser($beginUid);
		$journalCount = count($journalsAr);

		for ($i=0; $i<$journalCount; $i++)
		{
			$journalsAr[$i]['date'] = $c_all->convertTime($journalsAr[$i]['date']);
			$journalsAr[$i]['commentsCount'] = count($c_journal->selectComments($journalsAr[$i]['id']));
		}

		if (!$jrnId)
		{
			$jrnId = ($journalCount>0) ? $journalsAr[0]['id'] : 0;
  		}

		if (!$isOwner)
		{
			$c_journal->journalView($jrnId);
		}
	//}

	if ($isOwner)
	{
		if ($act == 'addjrnl')
		{
			// add new record
			$journal = array();
			$smarty->assign('act', 'savejrnl');
			$tpl = 'addjournal.tpl';
		}
		elseif ($act == 'savejrnl')
		{
			$post = $_POST;
			$post['user_id'] = $uid;
			$post['username'] = $userdata['username'];
			$jrn_id = $c_journal->insertJournal($post);
			header("Location: journal.php?jrn_id=$jrn_id");
		}
		elseif ($act == 'updjrnl')
		{
			$post = $_POST;
			$c_journal->updateJournal($post);
			header("Location: journal.php?jrn_id=$jrnId");
		}
		elseif ($act == 'ctrljrnl')
		{
			$tpl = 'ctrljournal.tpl';
		}
		elseif ($act == 'editjrnl')
		{
			$journal = $c_journal->selectOneJournal($jrnId);
			$smarty->assign('jrnId', $jrnId);
			$smarty->assign('act', 'updjrnl');
			$tpl = 'addjournal.tpl';
		}
		elseif ($act == 'deletejrnl')
		{
			$c_journal->deleteJournal($jrnId);
			$tpl = 'ctrljournal.tpl';
			header("Location: journal.php?jrn_act=ctrljrnl");
		}
	}
	else
	{
		// get user from db by i
		$act == 'read';
	}

	if ($act == 'addcomment')
	{
		$post = $_POST;
		$post['user_id'] = $userdata['user_id'];
		$post['username'] = $userdata['username'];
		$post['jrn_id'] = $jrnId;
		$c_journal->insertComment($post);
		header("Location: journal.php?jrn_id=$jrnId");
	}

	if ($act == 'readall')
	{
		$journalsAr = $c_journal->selectAllJournals();
		$journalCount = count($journalsAr);

		for ($i=0; $i<$journalCount; $i++)
		{
			$journalsAr[$i]['date'] = $c_all->convertTime($journalsAr[$i]['date']);
			$journalsAr[$i]['commentsCount'] = count($c_journal->selectComments($journalsAr[$i]['id']));
		}
		$tpl = 'alljournals.tpl';
	}

	if ($act == 'read')
	{
		$isJournal = true;
		if ($jrnId == 0)
		{
			$isJournal = false;
		}
		else
		{
			$journal = $c_journal->selectOneJournal($jrnId);
			$journal[0]['date'] = $c_all->convertTime($journal[0]['date']);

      // TODO: bbcode
			//include($phpbb_root_path . 'includes/bbcode.'.$phpEx);
			//$journal[0]['text'] = bbencode_second_pass($journal[0]['text'], '', true);

			$isOwner = ($journal[0]['user_id'] == $userdata['user_id']);

			$commentsAr = $c_journal->selectComments($jrnId);
			$commentsCount = count($commentsAr);
			for ($i=0; $i<$commentsCount; $i++)
			{
				$commentsAr[$i]['date'] = $c_all->convertTime($commentsAr[$i]['date'], 2);
			}
			$uid = $journal[0]['user_id'];
	// TODO:
  //		$user_data = get_userdata($uid);
			$smarty->assign('commentsAr', $commentsAr);
		}
		$smarty->assign('isJournal', $isJournal);
	}

  	$smarty->assign('uid', $uid);
  	$smarty->assign('isOwner', $isOwner);
  	$smarty->assign('isMember', $isMember);
    $smarty->assign('userdata', $user_data);
    $smarty->assign('journalsAr', $journalsAr);
    $smarty->assign('journal', $journal);

//  $smarty -> assign('rssnews2_ar',$rssnews2_ar);
  $smarty -> display("site/$tpl");
?>